const {NAME_AND_PASSWORD_IS_REQUIRED,PASSWORD_ERROR,USER_ERROR}=require('../error/errorType')
const serviceSql=require('../service/service')

const verifyLogin=async(ctx,next)=>{
    const {name,password}=ctx.request.body
    if(!name || !password){
        return ctx.app.emit('error',new Error(NAME_AND_PASSWORD_IS_REQUIRED),ctx)
    }
    let result=await serviceSql.login(name)
    if(!result.length){
        return ctx.app.emit('error',new Error(USER_ERROR),ctx)
    }
    let [userInfo]=result
    if(String(password)!=userInfo.password){
        return ctx.app.emit('error',new Error(PASSWORD_ERROR),ctx)
    }
    ctx.userInfo=userInfo
    await next()
}

module.exports={
    verifyLogin
}